<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>模块管理</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="format-detection" content="telephone=no">
		<script src="../js/global.js" type="text/javascript" charset="utf-8"></script>
	</head>

	<body>
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
			<legend>系統功能模块维护</legend>
		</fieldset>
		<div class="layui-container">
			<div class="layui-row">
				<div id="modulesTree" class="layui-col-md3">
					<!-- <div id="divModules" class="demo-tree demo-tree-box" style="width: 100%; height:100%; ">
					</div> -->
				</div>
				<div class="layui-col-md9">
					<div id="divModuleB">
						<button id="btnAddChild" type="button" class="layui-btn layui-btn-sm">添加子节点</button>
						<button id="btnChangeParent" type="button" class="layui-btn layui-btn-normal layui-btn-sm">设置父节点</button>
						<fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
							<legend><span id="spTitle">功能模块信息维护</span></legend>
						</fieldset>
						<form id="formSave" class="layui-form" action="" onsubmit="return false">
							<div class="layui-form-item">
								<label class="layui-form-label">父节点编号</label>
								<div class="layui-input-block">
									<input id="txtParentId" type="text" disabled="disabled" value="0" class="layui-input">
									<input id="hfParentId" type="hidden" name="parentId">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">节点编号</label>
								<div class="layui-input-block">
									<input id="hfId" type="hidden" name="mId">
									<input id="txtId" type="text" disabled="disabled" value="0" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">节点名称</label>
								<div class="layui-input-block">
									<input id="txtName" type="text" name="name" lay-verify="required" autocomplete="off" placeholder="请输入节点名称" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">路径</label>
								<div class="layui-input-block">
									<input id="txtPath" type="text" name="path" placeholder="请输入功能路径" autocomplete="off" class="layui-input">
								</div>
							</div>

							<div class="layui-form-item">
								<label class="layui-form-label">权重</label>
								<div class="layui-input-block">
									<input id="txtWeight" type="text" name="weight" lay-verify="required|number" placeholder="请输入权重(整数)" autocomplete="off" class="layui-input">
								</div>
							</div>

							<div class="layui-form-item">
								<div class="layui-input-block">
									<button class="layui-btn" lay-submit="" lay-filter="subData">保存</button>
									<button type="reset" class="layui-btn layui-btn-primary">重置</button>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</body>
	<script>
		layui.use(['tree','form','layer'],function(){
			var tree=layui.tree,
			form=layui.form,
			layer=layui.layer,
			$=layui.$;
			//页面加载完渲染树模型
			$(function(){//modules
			
			//获取该用户在模块页面拥有的权限
			$.ajax({
				type : "get",
				url : globalData.server+"modules/getPermissionsByModuleId",
				data:{
					id:globalData.getCurPageModuleId()
				},//需要传递到服务端的数据
				dataType:'json',//服务端响应数据类型
				xhrFields: {
					withCredentials: true
				},
				beforeSend: function(request) {
					request.setRequestHeader("token", globalData.getCurToken());
				},
				success : function(res) {
					//转换对象数组
					var permissionids=JSON.parse(globalData.getCurUserPermissions());
					//创建权限对象数组
					var permission_arr={};
					//遍历用户权限id
					for(var i=0;i<permissionids.length;i++){
						
						//遍历该模块的全部权限
						for(var j=0;j<res.length;j++){
							console.log(res[j]);
							 if(permissionids[i]==res[j].permissionid){
							 	permission_arr[i]=res[j];
							 }
						}
					}
					//遍历数据
					$.each(permission_arr,function(n,value){
						switch (value.permissionvalue){
							case 'module:queryModuleTree':
							$("#modulesTree").html("");
							var treeTarget='<div id="divModules" class="demo-tree demo-tree-box" style="width: 100%; height:100%; ">'+
								'</div>';
							$("#modulesTree").append(treeTarget);
								break;
						}
					});
					
				}
			});
			
			
				$.ajax({
					type : "get",
					url : globalData.server+"modules",
					data:{},//需要传递到服务端的数据
					dataType:'json',//服务端响应数据类型
					xhrFields: {
						withCredentials: true
					},
					beforeSend: function(request) {
						request.setRequestHeader("token", globalData.getCurToken());
					},
					success : function(res) {
						
							tree.render({
								id:'allModule',
								elem:'#divModules',
								data:res,
								edit:['del'],
								click:function(obj){
									var data=obj.data;
									var elem=obj.elem;
									var pid=0;
									//查询点击节点的模块信息
									$.ajax({
										type : "get",
										url : globalData.server+"modules/getById",
										data:{
											id:data.id
										},//需要传递到服务端的数据
										dataType:'json',//服务端响应数据类型
										xhrFields: {
											withCredentials: true
										},
										beforeSend: function(request) {
											request.setRequestHeader("token", globalData.getCurToken());
										},
										success : function(res) {
												if(res!=null){
													//var rs=JSON.parse(res.message);
													//获取信息写入数据
													$("#txtParentId").val(res.parentid);
													$("#txtId").val(res.id);
													$("#txtName").val(res.name);
													$("#txtPath").val(res.path);
													$("#txtWeight").val(res.weight);
													pid=res.parentid;
												}else{
													layer.alert('获取失败',{icon:2});
												}
												$("#spTitle").html('功能模块信息维护');
												
												//添加子节点事件
												$("#btnAddChild").click(function(){
													$("#spTitle").html('为【'+data.title+'】添加子节点');
													//清空输入框
													$("#txtParentId").val(res.id);
													$("#txtId").val(0);
													$("#txtName").val('');
													$("#txtPath").val('');
													$("#txtWeight").val('');
												});
												
												//设置父节点事件
												$("#btnChangeParent").click(function(){
													$("#spTitle").html('为【'+data.title+'】设置父节点');
													$("#txtParentId").removeAttr('disabled');
												});
												//监听保存按钮
												form.on('submit(subData)',function(obj){
													if($("#txtId").val()==0){
														//执行添加子节点
														active.addModule();
													}else{
														var cid=$("#txtParentId").val()
														alert(pid);
														alert(cid);
														//alert($("#txtParentId").val());
														//执行更新
														active.updateModule(data,elem,pid,cid);
													}
												});
											
										}
									});
									
								},operate:function(obj){
									var type=obj.type;
									var data=obj.data;
									if(type==='del'){
										layer.confirm('确定要删除['+data.title+']吗?',{icon:3},function(index){
											layer.close(index);
											$.ajax({
												type : "delete",
												url : globalData.server+"modules",
												data:{
													'id':data.id,
												},//需要传递到服务端的数据
												dataType:'json',//服务端响应数据类型
												xhrFields: {
													withCredentials: true
												},
												beforeSend: function(request) {
													request.setRequestHeader("token", globalData.getCurToken());
												},
												success : function(res) {
													if(res.success){
														layer.msg(res.msg,{icon:1,time:1500},function(index){
															layer.close(index);
														});
													}else{
														layer.msg(res.msg,{icon:2,time:1500});
													}
												}
											});
											
										});
									}
								}
							});
					}
				});
			
			});
			
			
			//定义js对象用来存储保存按钮的各种方法	
			var	active={
					//更新模块信息
					updateModule:function(data,elem,pid,cid){
						$("#hfParentId").val($("#txtParentId").val());
						$("#hfId").val($("#txtId").val());
						
						$.ajax({
							type : "put",
							url : globalData.server+"modules",
							data:{
								'id':$("#hfId").val(),
								'name':$("#txtName").val(),
								'parentid':$("#hfParentId").val(),
								'path':$("#txtPath").val(),
								'weight':$("#txtWeight").val()
							},//需要传递到服务端的数据
							dataType:'json',//服务端响应数据类型
							xhrFields: {
								withCredentials: true
							},
							beforeSend: function(request) {
								request.setRequestHeader("token", globalData.getCurToken());
							},
							success : function(res) {
								if(res.success){
									layer.msg('保存成功',{icon:1,time:2000},function(inex){
										layer.close(inex);
										//判断获取的父节点id是否和文本框里的id相同
										//如果相同则是修改,不同则是设置父节点
										//pid:原来的父id
										//cid:修改后的父id
										if(pid==cid){
											//修改树对应的json数据对象
											data.title=$("#txtName").val();
											//修改树的节点名称
											elem.find('.layui-tree-txt').html($("#txtName").val());
										}else{
											$("#txtParentId").attr("disabled",true);
											//重载数结构
											active.reload();
										}
									});
								}else{
									layer.msg('保存失败',{icon:2,time:2000});
								}
							}
						});
						
// 						$.post(globalData.server+'UpdateModule',
// 						$("#formSave").serialize()+'&token='+globalData.myToken
// //							'mId':$("#txtId").val(),
// //							'name':$("#txtName").val(),
// //							'parentId':$("#txtParentId").val(),
// //							'path':$("#txtPath").val(),
// //							'weight':$("#txtWeight").val(),
// //							'token':globalData.myToken
// 						,function(res){
							
// 						},"json");
					},
					//添加子节点
					addModule:function(){
						
						$.ajax({
							type : "post",
							url : globalData.server+"modules",
							data:{
								'name':$("#txtName").val(),
								'parentid':$("#txtParentId").val(),
								'path':$("#txtPath").val(),
								'weight':$("#txtWeight").val()
							},//需要传递到服务端的数据
							dataType:'json',//服务端响应数据类型
							xhrFields: {
								withCredentials: true
							},
							beforeSend: function(request) {
								request.setRequestHeader("token", globalData.getCurToken());
							},
							success : function(res) {
								if(res.success){
									layer.msg('添加成功',{icon:1,time:2000},function(inex){
										layer.close(inex);
										active.reload();
									});
								}else{
									layer.msg('添加失败',{icon:2,time:2000});
								}
							}
						});
						
					},
					//重载树组件
					reload:function(){
						$.ajax({
							type : "get",
							url : globalData.server+"modules",
							data:{},//需要传递到服务端的数据
							dataType:'json',//服务端响应数据类型
							xhrFields: {
								withCredentials: true
							},
							beforeSend: function(request) {
								request.setRequestHeader("token", globalData.getCurToken());
							},
							success : function(res) {
								//重载树结构
								tree.reload('allModule',{
									data:res
								});
							}
						});
					}
				}
		});
	
	</script>
</html>